<?php
namespace  Ekadmin\Controller;
use \Think\Controller;
use Think\Page;
use Vendor\Tree;

class GoodsController extends CommonController
{


        /**
         *@商品上架或者下架操作 
         */
        public function setStatus()
        {
            if(IS_POST){
                
                $goods = M('goods')->where("id=".I("id")." and uniacid=".$this->uniacid)->find();
                $save['is_shangjia'] = $goods['is_shangjia'] == 1 ?  2:1;
                $save['id'] = I('id');
                M('goods')->save($save);
                ajax_return(0,'操作成功');
            }
        }
        
        /**
         *@商品是不是置顶操作
         */
        public function setTop(){
            if(IS_POST){
            
                $goods = M('goods')->where("id=".I("id")." and uniacid=".$this->uniacid)->find();
            
                $save['is_top'] = $goods['is_top'] == 1 ?  2:1;
            
                $save['id'] = I('id');
            
                M('goods')->save($save);
            
                ajax_return(0,'操作成功');
            
            }
        }
        
        /**
         *@商品删除操作 
         */
        public function setDel(){
            if(IS_POST){
            
                $goods = M('goods')->where("id=".I("id")." and uniacid=".$this->uniacid)->find();
            
                $save['is_del'] = $goods['is_del'] == 1 ?  2:1;
            
                $save['id'] = I('id');
            
                M('goods')->save($save);
            
                ajax_return(0,'操作成功');
            
            }
        }
        
        
        /**
         *@显示商品列表 
         */
        public  function index()
        {            
            $where['uniacid'] = $this->uniacid;
            if(I('get.name')){
                $where['name'] = array('like','%'.I('get.name').'%');
            }
            if(I('get.classid')){
            
                $where['classid'] = I('get.classid');
            
            }
            
            if(I('get.code')){
            
                $where['code'] = I('get.code');
            
            }
            
            if(I('get.is_shangjia')){
            
                $where['is_shangjia'] = I('get.is_shangjia');
            
            }            
            if(I('get.is_top')){
            
                $where['is_top'] = I('get.is_top');
            
            }            
            if(I('get.is_del')){
            
                $where['is_del'] = I('get.is_del');
            
            }           
            $rs=M("goodsclass")->field("id,name")->where("uniacid=".$this->uniacid)->select();
            $this->info=$rs;
            $obj = M('goods');
            $count = $obj->where($where)->count();
            $page = new Page($count);
            $list = $obj->where($where)->limit($page->firstRow.','.$page->listRows)->order("id desc")->select();
            
            for($i=0;$i<count($list);$i++){
                $rs=M("goodsclass")->field("id,name")->where("id=".$list[$i]["classid"]." and uniacid=".$this->uniacid)->find();
                $list[$i]["classid"]=$rs["name"];

                
            }
            $this->assign('page',$page->show());
            $this->assign('count',$count);
            $this->assign('list',$list);
            
            //echo "<pre>";print_r($list);echo "</pre>";
            $this->display();
        }

        /**
         *@显示商品修改页面 
         */
        public function  edit()
        {
            $data = M('goods')->find(I('get.id'));
            $goodscate = M('goodsclass')->field('id,name')->order('sort')->where("uniacid=".$this->uniacid)->select();
            $tree = new Tree($goodscate);
            $str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
            $goodscate = $tree->get_tree(0, $str, $data['classid']);
            $this->assign('goodscate', $goodscate);//导航
            $imgs = explode('|',$data['imgs']);
            $this->assign('flag',1);
            $this->assign('url',U("Upload/saveimg_kind"));
            $this->assign('data',$data);
            $this->assign('imgs',$imgs);
            $this->display('form');
        }

        /**
         *@商品入库显示页面 
         */
        public function add()
        {

            $goodscate = M('goodsclass')->field('id,name')->where("uniacid=".$this->uniacid)->order('sort')->select();

            $tree = new Tree($goodscate);
            $str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
            $goodscate = $tree->get_tree(0, $str, 0);
            $this->assign('goodscate', $goodscate);//导航
            $this->assign('flag',1);
            $this->assign('url',U("Upload/saveimg_kind"));
            
            $this->code="SN".date("His").rand(1,9).rand(1,9).rand(1,9).rand(1,9);
            $this->display('form');
        }        /**
         *@商品修改提交操作 
         */
        public function update()
        {

            if(IS_POST){
                $param = I('post.');
                $param['price']   = doubleval($param['price']);
                if(!$param['thumb']){
                    ajax_return(1,'请上传商品缩略图');
                }
                //$param['start_time'] = strtotime($param['start_time']);                //$param['end_time'] = strtotime($param['end_time']);
                
                if(count($param['summary_imgs'])<1){
                    ajax_return(1,'请上传商品轮播图');
                }
                $param['imgs'] = implode('|',$param['summary_imgs']);
                unset($param['summary_imgs']);
                
                if(!M('goodsclass')->where(['id'=>$param['classid'],'pid'=>0,"uniacid"=>$this->uniacid])->find()){
                    ajax_return(1,'请选择二级分类');
                }
                
                if($param['id']){
                    $ar=M("goods")->where("id!=".$param["id"]." and code='".$param["code"]."' and uniacid=".$this->uniacid)->select();
                    if($ar){
                        ajax_return(1,'商品编码已存在');
                    }
                }
              //  $param['desc'] = str_replace('/addons/yb_fxyx/','https://'.$_SERVER['SERVER_NAME'].'/addons/yb_fxyx/',$param['desc']);
                if(intval($param["fee"])<=0){
                    $rs=M("config")->where("uniacid='".$this->uniacid."'")->find();
                    $param["fee"]=$rs["fee"];
                }

                $obj = M('goods');
                if($param['id']){
                    $param['updatetime'] = time();
                    $obj->save($param);
                }else{
                    $param['addtime'] = time();
                    $param['uniacid'] = $this->uniacid;
                    $obj->add($param);
                }
                ajax_return(0,'保存成功',U('index'));
            }
        }




    public function del()
    {
        if(IS_POST){
            $param = I('id');
            if(is_array($param)){
                $map['id'] = array('in',$param);
            }else{
                $map['id'] = $param;
            }
            
            $map["uniacid"]=$this->uniacid;
            
            M('goods')->where($map)->delete();
            
            ajax_return(0,'删除成功');
        }
    }


}